#include "updatedbthread.h"
#include <QProcess>
#include <QTextStream>
#include <QDebug>

UpdatedbThread::UpdatedbThread(QObject *parent)
    : QThread(parent)
{
    mWorker = new UpdatedbWorker;
    mWorker->moveToThread(this);
}

UpdatedbThread::~UpdatedbThread()
{
    delete mWorker;
}

UpdatedbWorker* UpdatedbThread::worker()
{
    return mWorker;
}

UpdatedbWorker::UpdatedbWorker()
{

}

UpdatedbWorker::~UpdatedbWorker()
{

}

void UpdatedbWorker::updatedb()
{
    emit updating();

    QProcess process;
    process.start("updatedb");
    process.waitForFinished(-1);

    QByteArray errorOutput = process.readAllStandardError();
    if (!errorOutput.isEmpty())
        qDebug().noquote() << "updatedb:" << errorOutput;

    QByteArray output = process.readAllStandardOutput();
    if (!output.isEmpty())
        qDebug().noquote() << "updatedb:" << output;


    if (process.exitCode() != 0)
        emit error(errorOutput);

    emit finished();
}
